package com.reebake.ideal.infra.controller;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.mybatis.core.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.reebake.ideal.infra.entity.DataDict;
import com.reebake.ideal.infra.service.DataDictService;
import com.reebake.ideal.infra.vo.DataDictQueryParamVO;
import com.reebake.ideal.infra.vo.DataDictVO;

 /**
 * 数据字典控制层
 */
@Tag(name = "数据字典对象功能接口")
@RestController
@RequestMapping("/data/dict")
public class DataDictController {
    @Autowired
    private DataDictService dataDictService;
    
    /** 
     * 分页查询
     *
     * @param queryParam 查询参数
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("page")
    public PageResult<DataDictVO> page(@RequestBody DataDictQueryParamVO queryParam) {
        LambdaQueryWrapper<DataDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(queryParam.getDictCode()), DataDict::getDictCode, queryParam.getDictCode());
        queryWrapper.like(StrUtil.isNotBlank(queryParam.getDictName()), DataDict::getDictName, queryParam.getDictName());
        queryWrapper.eq(StrUtil.isNotBlank(queryParam.getStatus()), DataDict::getStatus, queryParam.getStatus());
        
        queryParam = dataDictService.page(queryParam, queryWrapper);
        
        return queryParam.to(DataDictVO.class);
    }
    
    /** 
     * 保存或更新数据
     *
     * @param dataDict 实例对象
     */
    @Operation(summary = "保存或更新数据")
    @PostMapping("save")
    public void save(@RequestBody DataDictVO dataDict) {
        dataDictService.saveOrUpdate(BeanUtil.copyProperties(dataDict, DataDict.class));
    }
    
    /** 
     * 通过主键批量删除
     *
     * @param dataDictIds 主键
     */
    @Operation(summary = "通过主键批量删除")
    @PostMapping("remove-by-ids")
    public void removeByIds(@RequestBody List<String> dataDictIds) {
        dataDictService.removeByIds(dataDictIds);
    }
}